package media;

import java.io.IOException;
import java.util.ArrayList;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;

/**
 * 封装播放功能，在这里控制了播放状态，调用处无需考虑
 */
public class MediaPlayerWrapper implements OnCompletionListener{
	private MediaPlayer mPlayer;
	private String mFilePath;
	private String mName;
	private PlayStatus mPlayStatus;
	private ArrayList<MediaListener> mList = null;
	private static MediaPlayerWrapper sInstance = null;

	/**
	 * 播放状态
	 */
	public enum PlayStatus {
		IDLE, PLAYING, PAUSE, STOP, ERROR;
	}

	synchronized public static MediaPlayerWrapper getInstance() {
	    if (null == sInstance) {
	        sInstance = new MediaPlayerWrapper();
	    }
	    
	    return sInstance;
	}
	
	public static void Destory() {
	    if (null != sInstance) {
	        sInstance.release();
	        sInstance.clear();
	    }
	    sInstance = null;
	}
	private MediaPlayerWrapper() {
		mPlayStatus = PlayStatus.IDLE;
	}

	public void setFilePath(String filePath) {
	    mFilePath = filePath;
	}
	
	public String getFilePath() {
        return mFilePath;
    }
	
	public void setName(String value) {
	    mName = value;
    }
    
    public String getName() {
        return mName;
    }
	
	public PlayStatus getPlayStatus() {
	    return mPlayStatus;
	}
	
	public void play(String filePath) {
	    if (mPlayStatus != PlayStatus.IDLE) {
	        release();
	    }
	    setFilePath(filePath);
	    play();
	}
	
	public void registerMediaListener(MediaListener listener) {
	    if (null == mList) {
	        mList = new ArrayList<MediaListener>();
	    }
	    
	    if (!mList.contains(listener)) {
	        mList.add(listener);
	    }
	}
	
	public void removeMediaListener(MediaListener listener) {
	    if (null != mList) {
	        mList.remove(listener);
	    }
	}
	
	public void play() {
	    onPlay();
		if (mPlayer == null) {
		    mPlayer = new MediaPlayer();
		    mPlayer.setOnCompletionListener(this);
		}

		if (mPlayStatus == PlayStatus.IDLE) {
			try {
				mPlayer.setDataSource(mFilePath);
				mPlayer.prepare();
				mPlayer.start();
				mPlayStatus = PlayStatus.PLAYING;
			} catch (IOException e) {
				e.printStackTrace();
				release();
			}
		} else if (mPlayStatus == PlayStatus.PAUSE) {
			mPlayer.start();
			mPlayStatus = PlayStatus.PLAYING;
		} else if (mPlayStatus == PlayStatus.STOP) {
			try {
				mPlayer.prepare();
				mPlayer.start();
				mPlayStatus = PlayStatus.PLAYING;
			} catch (IllegalStateException e) {
				e.printStackTrace();
				release();
			} catch (IOException e) {
				e.printStackTrace();
				release();
			}
		} else if (mPlayStatus == PlayStatus.PLAYING) {
			mPlayer.start();
		} else {
		    
		}
	}

	public void pause() {
	    onPause();
		if (mPlayer != null && mPlayer.isPlaying()) {
			mPlayer.pause();
			mPlayStatus = PlayStatus.PAUSE;
		}
	}

	public void stop() {
		if (mPlayer != null) {
			if (mPlayStatus == PlayStatus.PLAYING
					|| mPlayStatus == PlayStatus.PAUSE) {
				mPlayer.seekTo(0);
				mPlayer.stop();
				mPlayStatus = PlayStatus.STOP;
			}
		}
	}

	public void reset() {
        mPlayStatus = PlayStatus.IDLE;
        mPlayer.reset();
    }
	
	/**
	 * 释放资源
	 */
	public void release() {
		if (mPlayer != null) {
			mPlayer.release();
		}
		mFilePath = null;
		mName = null;
		mPlayer = null;
		mPlayStatus = PlayStatus.IDLE;
		onRelease();
	}
	
	public void clear() {
	    if (null != mList) {
	        mList.clear();
	    }
	    mList = null;
	}
	
	public interface MediaListener {
	    public void onMediaPlay();
	    public void onMediaPause();
	    public void onMediaRelease();
	    public void onMediaCompletion();
	    
	}
	
	private void onPlay() {
	    ArrayList<MediaListener> list = new ArrayList<MediaListener>();
        list.addAll(mList);
        if (list.size() > 0) { 
            for (MediaListener listener : list) {
                try {
                    listener.onMediaPlay();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
	}
	
	private void onPause() {
	    ArrayList<MediaListener> list = new ArrayList<MediaListener>();
        list.addAll(mList);
        if (list.size() > 0) { 
            for (MediaListener listener : list) {
                try {
                    listener.onMediaPause();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
	}
	
	private void onRelease() {
        ArrayList<MediaListener> list = new ArrayList<MediaListener>();
        list.addAll(mList);
        if (list.size() > 0) { 
            for (MediaListener listener : list) {
                try {
                    listener.onMediaRelease();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
	
    @Override
    public void onCompletion(MediaPlayer mp) {
        ArrayList<MediaListener> list = new ArrayList<MediaListener>();
        list.addAll(mList);
        if (list.size() > 0) { 
            for (MediaListener listener : list) {
                try {
                    listener.onMediaCompletion();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
